//
//  LXJVisitorView.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/27.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit

/// 访客视图
/// 加载登录页的代理协议
protocol LXJVisitorViewDelegate: NSObjectProtocol {
    func loadLoginController()
}

class LXJVisitorView: UIView {
    
    // MARK: 控件的懒加载
    /// 底部旋转动画视图
    private lazy var cycleImageView: UIImageView = {
        var imageView = UIImageView()
        /// 设置图片
        imageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return imageView
    }()
    
    /// 底部旋转视图上的遮罩视图
    private lazy var maskImageView: UIImageView = {
        var imageView = UIImageView()
        /// 设置图片
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return imageView
    }()
    
    /// 遮罩视图上的小房子视图
    private lazy var homeImageView: UIImageView = {
        var imageView = UIImageView()
        /// 设置图片
        imageView.image = UIImage(named: "visitordiscover_feed_image_house")
        return imageView
    }()
    
    /// 访客描述信息
    private lazy var messageLabel: UILabel = {
        var label = UILabel()
        /// 字号
        label.font = UIFont.systemFont(ofSize: 13)
        /// 字体颜色
        label.textColor = UIColor.darkGray
        /// 文字对齐方式
        label.textAlignment = .center
        /// 自动换行
        label.numberOfLines = 0
        /// 文字内容
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
    /// 注册摁扭
    private lazy var  registerButton: UIButton  = {
        var button = UIButton()
        /// 文字内容
        button.setTitle("注册", for:.normal)
        /// 字号
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        /// 字体颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        /// 切图
        var image = UIImage(named: "common_button_white_disable")
        image = image!.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: .stretch)
        /// 摁扭的背景图片
        button.setBackgroundImage(image, for: .normal)
        /// 取消高亮效果
        button.adjustsImageWhenHighlighted = false
        /// 监听摁扭的点击事件
        button.addTarget(self, action: #selector(registerAndLoginAction), for: .touchUpInside)
        
        return button
    }()
    
    /// 登录摁扭
    private lazy var loginButton: UIButton  = {
        var button = UIButton()
        /// 文字内容
        button.setTitle("登录", for:.normal)
        /// 字号
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        /// 字体颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        /// 切图
        var image = UIImage(named: "common_button_white_disable")
        image = image!.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: .stretch)
        /// 摁扭的背景图片
        button.setBackgroundImage(image, for: .normal)
        /// 取消高亮效果
        button.adjustsImageWhenHighlighted = false
        /// 监听摁扭的点击事件
        button.addTarget(self, action: #selector(registerAndLoginAction), for: .touchUpInside)
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    func setupUI() -> Void {
        /// 设置访客页面的背景色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        /// 添加子控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        /// 特别说明:记得先添加控件再做自动布局
        
        /// 自动布局
        //        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        
        /// 使用SnapKit自动布局
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalTo(maskImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp.bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.centerY.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
    }
    
    // MARK: 开启底部循环视图的动画
    private func startCycyleViewAnimation() -> Void {
        /// 创建动画对象
        let cycAnimaton = CABasicAnimation(keyPath: "transform.rotation")
        /// 目标值
        cycAnimaton.toValue = 2 * Double.pi
        /// 动画时长
        cycAnimaton.duration = 20
        /// 重复次数
        cycAnimaton.repeatCount = MAXFLOAT
        /// 失去焦点
        cycAnimaton.isRemovedOnCompletion = false
        /// 添加到循环视图的图层上
        cycleImageView.layer.add(cycAnimaton, forKey: nil)
    }
    
    // MARK: 更新访客页面
    func updateVisitorView(imageName: String?, message: String?) -> Void {
        /// 如果imageName message有值,表示不是首页
        if let imgName = imageName, let mesName = message {
            /// 更新图片
            homeImageView.image = UIImage(named: imgName)
            /// 更新消息内容
            messageLabel.text = mesName
            /// 隐藏循环视图
            cycleImageView.isHidden = true
        } else{
            /// 表示是首页
            /// 开启动画
            startCycyleViewAnimation()
        }
    }
    
    // MARK: 代理对象和闭包
    /// 记得弱引用,防止代理对象的循环引用
    weak var delegate: LXJVisitorViewDelegate?
    /// 设置闭包属性
    var registerAndLoginClosure: (() -> ())?
    
    // MARK: 登录注册摁扭的点击事件
    @objc private func registerAndLoginAction() -> Void {
        /// 执行代理方法
        delegate?.loadLoginController()
        /// 执行闭包
        registerAndLoginClosure?()
    }
}
